package com.hakim.wfb.web.controller.custom;

import com.hakim.wfb.admin.params.user.PwdRestParam;
import com.hakim.wfb.admin.service.api.CustomerService;
import com.hakim.wfb.customer.params.trans.TransferCreditPointParameter;
import com.hakim.wfb.customer.params.user.ActivateCustomerParameter;
import com.hakim.wfb.customer.params.user.GetUserDetailParameter;
import com.hakim.wfb.customer.params.user.QueryCustomerWebPageParameter;
import com.hakim.wfb.customer.result.user.UserDetailView;
import com.hakim.wfb.customer.result.user.UserView;
import com.hakim.wfb.customer.service.UserService;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by walkman on 2017/7/21.
 */
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {

    @Autowired
    private UserService userService;

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "/queryCustomer", name = "会员列表", method = RequestMethod.POST)
    public ListResultEx<UserView> queryUser(QueryCustomerWebPageParameter parameter, HttpServletRequest request) {

        return userService.queryUserList(parameter);
    }

    @RequestMapping(value = "/activeCustomer", name = "用户激活", method = RequestMethod.POST)
    public ResultEx activateCustomer(ActivateCustomerParameter parameter, HttpServletRequest request) {
        ResultEx resultEx = new ResultEx();
        try {
            SessionHandler handler = new SessionHandler();
            SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
            parameter.setUuid(identity.getUserId().toString());
            return userService.activateCustomer(parameter);
        } catch (Exception e) {
            return resultEx.makeFailedResult("用户激活失败");
        }
    }

    @RequestMapping(value = "/userinfo", name = "查询用户信息", method = RequestMethod.POST)
    public ObjectResultEx<UserDetailView> getUserDetail(GetUserDetailParameter param, HttpServletRequest request) {

        ObjectResultEx<UserDetailView> result = new ObjectResultEx<>();
        try {
//            SessionHandler handler = new SessionHandler();
//            SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
//            param.setSessionIdentity(identity);
//            param.setUuid(identity.getUuid());
            return userService.getUserDetail(param);
        } catch (Exception e) {
//            logger.error("", e);
            return result.makeFailedResult("");
        }
    }

    /**
     * 会员积分划拨
     *
     * @param parameter
     * @param request
     * @return
     */
    @RequestMapping(value = "/sysTranPoint", name = "积分划拨", method = RequestMethod.POST)
    public ResultEx sysTranPoint(TransferCreditPointParameter parameter, HttpServletRequest request) {
        ResultEx resultEx = new ResultEx();
        try {
            SessionHandler handler = new SessionHandler();
            SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
            parameter.setSysUuid(identity.getUserId().toString());
            return userService.sysTransferPoint(parameter);
        } catch (Exception e) {
            return resultEx.makeFailedResult("积分划拨失败");
        }
    }

    /**
     * 修改密码
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/pwdreset", name = "修改密码", method = RequestMethod.POST)
    public ResultEx pwdReset(PwdRestParam param, HttpServletRequest request) {
        /*SessionHandler handler = new SessionHandler();
        SessionIdentity identity = handler.getSessionIdentity(request, SessionIdentity.class);
        param.setSessionIdentity(identity);
        param.setUserId(identity.getUuid());*/
        return customerService.pwdReset(param);
    }
}
